package com.java.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.java.DictFeignClient;
import com.java.mapper.PatientMapper;
import com.java.model.cmn.DictEnum;
import com.java.model.user.Patient;
import com.java.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper,Patient> implements PatientService {

    //调用数据字典根据 省市证件代号 查询 省 市 联系人证件类型
    @Autowired
    private DictFeignClient dictFeignClient;

    @Autowired
    private PatientMapper patientMapper;

    @Override
    public List<Patient> findList(Long userId) {
        //根据id查询出人员列表
        QueryWrapper<Patient> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<Patient> list = patientMapper.selectList(wrapper);
        //循环得到每一个用户
//        list.stream().forEach(item->{
//            //查询每一项 将省 市 联系人证件代码变成名称
//            packgePatien(item);
//        });
        return list;
    }

    @Override
    public Patient getPatientById(Long id) {
        Patient patient = packgePatien(baseMapper.selectById(id));
        //Patient patient =patientMapper.selectById(id);
        return patient;
    }

    //找出 province_code city_code  district_code contacts_certificates_type 变成名称
    private Patient packgePatien(Patient patient) {
        //注册人的证件名称
        String name = dictFeignClient
                .getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificatesType());
        //联系人的证件名称
        String concatName = dictFeignClient
                .getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getContactsCertificatesType());
        //调用远程的数据字典方法获取名称
        String provinceName = dictFeignClient.getName(patient.getProvinceCode());
        String cityName = dictFeignClient.getName(patient.getCityCode());
        String districtName = dictFeignClient.getName(patient.getDistrictCode());
        //封装到类中
        patient.getParam().put("province",provinceName);
        patient.getParam().put("city",cityName);
        patient.getParam().put("district",districtName);
        patient.getParam().put("fullAddress",provinceName+cityName+districtName);
        patient.getParam().put("CertificatesTypeStr",name);
        patient.getParam().put("concatCertificatesTypeStr",concatName);
        return patient;
    }


}
